package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import xyz.hubery.enums.CodeEnum;
import xyz.hubery.mapper.AdminMapper;
import xyz.hubery.model.BusException;
import xyz.hubery.pojo.Admin;
import xyz.hubery.pojo.Permission;
import xyz.hubery.service.AdminService;

import java.util.List;

@DubboService
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public void add(Admin admin) {
        // 根据用户名查找用户
        Admin admin1 = this.findByAdminName(admin.getUsername());
        if (admin1 != null) {
            throw new BusException(CodeEnum.INSERT_USER_ERROR);
        }
        adminMapper.insert(admin);
    }

    @Override
    public void update(Admin admin) {
        // 查询当前用户信息
        Admin existingAdmin = adminMapper.selectById(admin.getAid());
        if (existingAdmin == null) {
            throw new BusException(CodeEnum.USER_NOT_EXIST);
        }

        // 如果用户名发生变更，需要检查新用户名是否已存在
        if (!existingAdmin.getUsername().equals(admin.getUsername())) {
            Admin adminWithName = this.findByAdminName(admin.getUsername());
            if (adminWithName != null && !adminWithName.getAid().equals(admin.getAid())) {
                throw new BusException(CodeEnum.INSERT_USER_ERROR); // 用户名已存在
            }
        }

        // 密码处理逻辑
        if (!StringUtils.hasText(admin.getPassword())) {
            admin.setPassword(existingAdmin.getPassword());
        }

        // 执行更新
        adminMapper.updateById(admin);
    }


    @Override
    public void delete(Long id) {
        // 删除管理员-角色信息
        adminMapper.deleteAdminRoles(id);
        // 删除管理员
        adminMapper.deleteById(id);
    }

    @Override
    public Admin findById(Long id) {
        return adminMapper.selectAdminWithRolesAndPermissions(id);
    }

    @Override
    public Page<Admin> search(int page, int size) {
        Page<Admin> adminIPage = new Page<Admin>(page,size);
        return adminMapper.selectPage(adminIPage,null);
    }

    @Override
    public void updateRoleToAdmin(Long aid, Long[] rids) {
        // 删除该管理员原来的角色
        adminMapper.deleteAdminRoles(aid);
        // 添加新的角色
        adminMapper.addAdminRoles(aid,rids);
    }


    @Override
    public Admin findByAdminName(String username) {
        return adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername,username));
    }

    @Override
    public List<Permission> findAllPermission(String username) {
      return   adminMapper.selectAdminPermissionsByName(username);
    }
}
